<?php

namespace App\Task;


class test extends TaskBase
{
    public function funcHandle($index)
    {
//        \co::sleep(3);
//        var_dump($index);

//        $s = $this->db()->rawQuery('SELECT SLEEP(3)');
////        \co::sleep(5);


        $table_name = 'wx_app.t_t_test';

        $b = \h::today();
        $e = \h::today()->addDay()->subSecond();
        $count = $this->db()->whereBetween('t_create_date', [
            $b->toDateTimeString(),
            $e->toDateTimeString()
        ])->count($table_name);
        if ($count >= 1000)
        {
            $r = $this->redis()->get('task|insert|' . \h::today()->toDateString());
            if (\h::funcStrIsNullOrEmpty($r))
            {
                $arr_first = $this->db()
                    ->where('t_create_date', \h::today()->toDateString(), '>')
                    ->getOne($table_name);
                $arr_last = $this->db()
                    ->orderBy('t_create_date')
                    ->getOne($table_name);

                $begin_time = \Carbon::parse($arr_first['t_create_date']);
                $end_time = \Carbon::parse($arr_last['t_create_date']);

                $use_s = $end_time->diffInSeconds($begin_time);

                $this->redis()->set('task|insert|' . \h::today()->toDateString(), "使用时间：{$use_s}秒");
            }

            return 0;
        }

        $this->db()->insert($table_name, [
            't_id' => \h::funcGetID(),
        ]);
//
        return 1;
    }

    public function funcDone()
    {
    }

}